package com.copa.controller;

import com.copa.service.ChatAssistant;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author copa
 * @date 2025/8/19 00:15
 * @description: https://docs.langchain4j.dev/tutorials/rag#rag-flavours-in-langchain4j
 */
@RestController
@Slf4j
public class RAGController {

    @Resource
    private InMemoryEmbeddingStore<TextSegment> embeddingStore;

    @Resource
    private ChatAssistant chatAssistant;

    @GetMapping(value = "/rag/add")
    public String testAdd() throws FileNotFoundException {
        // Document document = FileSystemDocumentLoader.loadDocument("C:\Users\copa1\Downloads\阿里巴巴泰山版java开发手册.pdf");
        FileInputStream fileInputStream = new FileInputStream("C:\\Users\\copa1\\Downloads\\test.xlsx");
        Document document = new ApacheTikaDocumentParser().parse(fileInputStream);
        EmbeddingStoreIngestor.ingest(document, embeddingStore);
        String result = chatAssistant.chat("错误码00000和A0001分别是什么");
        System.out.println(result);
        return result;
    }
}
